package com.huizhonglm.diap.ui.views;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.widget.pickerView.ArrayWheelAdapter;
import com.huizhonglm.diap.widget.pickerView.OnWheelScrollListener;
import com.huizhonglm.diap.widget.pickerView.WheelView;

/**
 * 城市地区选择
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2014-12-17 16:16
 */
public class BankPickerView extends PopupWindow {

    private View parent = null;
    private Activity mActivity = null;
    private WheelView mWheelView = null;

    private String[] bankArray = {"招商银行", "工商银行", "建设银行", "农业银行", "交通银行", "浦发银行", "中国银行", "光大银行", "民生银行"};

    private OnBankSelectedListener mListener;

    private PopupMaskView mask;

    private boolean isOnScrolling = false;

    /**
     * 日期选择器
     *
     * @param activity 上下文
     * @param parent   显示锚点View
     * @param l        选择监听器
     */
    public BankPickerView(Activity activity, View parent, OnBankSelectedListener l) {
        super(activity);
        mask = new PopupMaskView(activity, this);
        this.parent = parent;
        this.mActivity = activity;
        this.mListener = l;
        initPickerView();
    }

    public void show(String bankName) {
        mask.showAtLocation(parent);
        if (!EmptyUtil.isEmpty(bankName)){
            setDefaultSelected(bankName);
        } else {
            setDefaultSelected(bankArray[0]);
        }
        if (!this.isShowing()) {
            this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        }
    }

    /**
     * 初始化pickerView
     */
    private void initPickerView() {
        View contentView = View.inflate(mActivity, R.layout.view_bank_picker, null);
        mWheelView = (WheelView) contentView.findViewById(R.id.bank_wheel);
        Button confirmBtn = (Button) contentView.findViewById(R.id.common_btn);
        Button cancelBtn = (Button) contentView.findViewById(R.id.cancel_btn);
        contentView.setFocusableInTouchMode(true);
        contentView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((keyCode == KeyEvent.KEYCODE_BACK) && (isShowing())) {
                    dismiss();
                    return true;
                }
                return false;
            }
        });
        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isOnScrolling) {
                    dismiss();
                    if (mListener != null) {
                        mListener.onBankSelected(bankArray[mWheelView.getCurrentItem()]);
                    }
                }
            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isOnScrolling) {
                    dismiss();
                }
            }
        });

        mWheelView.setAdapter(new ArrayWheelAdapter<>(bankArray));
        mWheelView.setLabel("");
        mWheelView.addScrollingListener(getScrollListener());

        setFocusable(true);
        setWheelColumnNum(1);
        setContentView(contentView);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new BitmapDrawable());
        setAnimationStyle(R.style.bottom_view_anim_style);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                mask.dismiss();
            }
        });
        update();
    }

    private OnWheelScrollListener getScrollListener() {
        return new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {
                isOnScrolling = true;
            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                isOnScrolling = false;
            }
        };
    }

    private void setWheelColumnNum(int columnNum) {
        if (mWheelView != null) {
            mWheelView.setColumnNum(columnNum);
        }
    }

    private void setDefaultSelected(String name) {
        int index = 0;
        if (!EmptyUtil.isEmpty(name)) {
            int i = 0;
            for (String bank : bankArray) {
                if (bank.equals(name)) {
                    index = i;
                    break;
                }
                i++;
            }
        }
        mWheelView.setCurrentItem(index);
    }

    /**
     * 确认所选择的数据
     *
     * @author LiXiaoLong
     * @version 1.0
     * @since 2013年7月30日 下午1:13:38 (1.0)
     */
    public interface OnBankSelectedListener {
        /**
         * @param bankName 银行名称
         */
        void onBankSelected(String bankName);
    }
}